//
//  TimeTableXMLParser.m
//  Mehfile Zainab
//
//  Created by Sitara Badourali on 10/06/12.
//  Copyright (c) 2012 Mehfile Zainab. All rights reserved.
//

#import "TimeTableXMLParser.h"
#import "MZTimeTable.h"

@implementation TimeTableXMLParser

- (TimeTableXMLParser *) initXMLDefaultParser:(MZTimeTableViewController*)aViewController queriedDate:(NSDate*)aQueryDate {

    [super init];

    defaultViewController = aViewController;

    currentElementValue = [[NSMutableString alloc]init];

    queryDate = aQueryDate;

    return self;
    
}

- (TimeTableXMLParser *) initXMLParser:(MZTimeTableSegmentedViewController*)aViewController queriedDate:(NSDate*)aQueryDate {
    
    [super init];
    
    viewController = aViewController;
    
    currentElementValue = [[NSMutableString alloc]init];
    
    queryDate = aQueryDate;
    
    return self;
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict {

    //NSLog(@"Processing Element: %@", elementName);
    
    NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init] autorelease];
    [dateFormat setDateFormat:@"d"];
    //NSString *currentDay = [dateFormat stringFromDate:[NSDate date]];
    NSString *currentDay = [dateFormat stringFromDate:queryDate];
    
    if([elementName isEqualToString:@"timetable"]) {
        if (!viewController.mzTimeTableArray) {
            viewController.mzTimeTableArray = [[NSMutableArray alloc] init];
        }
    }
    else if([elementName isEqualToString:@"day"]) {
        timetable = [[MZTimeTable alloc] init];
        timeValues = [[NSMutableArray alloc] init];
        NSString *parsedDay = [attributeDict objectForKey:@"number"];
        //NSLog(@"Processing Attribute: %@", parsedDay);
        if ([parsedDay isEqualToString:currentDay]) {
            NSLog(@"The current day matches the tag!");
            NSDateFormatter *completeDateFormat = [[[NSDateFormatter alloc]init] autorelease];
            [completeDateFormat setDateFormat:@"d MMMM yyyy"];
            [completeDateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"]];
            //NSString *currentCompleteDay = [completeDateFormat stringFromDate:[NSDate date]];
            NSString *currentCompleteDay = [completeDateFormat stringFromDate:queryDate];
            timetable.mzNamazDate = currentCompleteDay;
        }
    }
    
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

    //NSLog(@"Processing Value: %@", currentElementValue);
    
    [currentElementValue appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    
    //NSLog(@"Processing Value: %@", currentElementValue);
    
}



- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    
    //NSLog(@"Processing Value: %@", currentElementValue);
    
    NSDateFormatter *completeDateFormat = [[[NSDateFormatter alloc]init] autorelease];
    [completeDateFormat setDateFormat:@"d MMMM yyyy"];
    [completeDateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"]];
    //NSString *currentCompleteDay = [completeDateFormat stringFromDate:[NSDate date]];
    NSString *currentCompleteDay = [completeDateFormat stringFromDate:queryDate];
    
    if([elementName isEqualToString:@"timetable"])
        return;
    
    if([elementName isEqualToString:@"day"]) {
        if ([timetable.mzNamazDate isEqualToString:currentCompleteDay]) {
        	[timetable setNamazTimeDictionaryWithValuesArray:timeValues];
            [viewController.mzTimeTableArray addObject:timetable];
            [timetable release];
            timetable = nil;
        }
    }
    else {
        if ((currentElementValue) && ([[currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ] length]!=0)) {
            if([elementName isEqualToString:@"fajr"]) {
                [timeValues addObject:currentElementValue];
            }
            if([elementName isEqualToString:@"lever"]) {
                [timeValues addObject:currentElementValue];
            }   
            if([elementName isEqualToString:@"zohr"]) {
                [timeValues addObject:currentElementValue];
            }
            if([elementName isEqualToString:@"coucher"]) {
                [timeValues addObject:currentElementValue];
            }    
            if([elementName isEqualToString:@"magrib"]) {
                [timeValues addObject:currentElementValue];
            } 
        }
    }
    
    [currentElementValue release];
    currentElementValue = [[NSMutableString alloc]init];
    
}


- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
    NSLog(@"Error %i, Description: %@, Line: %i, Column: %i", 
          [parseError code],
          [[parser parserError] localizedDescription], 
          [parser lineNumber],
          [parser columnNumber]);
}

- (void) dealloc {
    
    [timetable release];
    [currentElementValue release];
    [super dealloc];
}

@end
